<template>
  <div class="residentBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <h3>公告列表</h3>
      </div>
      <div class="text item">
        <span>新闻</span>
        <el-input placeholder="请输入新闻名" v-model="key" clearable></el-input>
        <span>公告类型</span>
      </div>
      <el-select v-model="type" clearable placeholder="请选择">
        <el-option
          v-for="item in seleceList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <el-button type="primary" @click="getNews">查询</el-button>
    </el-card>
    <div class="btn">
      <el-button type="primary" @click="addNews">添加</el-button>
      <el-button type="danger" @click="NewsDelAll">批量删除</el-button>
    </div>
    <newsDialog
      :centerDialogVisible="centerDialogVisible"
      :isEdit="isEdit"
      :item="item"
      @close="close('centerDialogVisible')"
    ></newsDialog>

    <tableList
      :list="list"
      :tableList="tableList"
      @Newsdel="Newsdel"
      @updata="updata"
      @multipleSelection="multipleSelection"
    ></tableList>

    <Pagination
      :total="total"
      :pageSize="pageSize"
      @changePsize="handleSizeChange"
      @changePage="handleCurrentChange"
    />
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import newsDialog from "../components/news/Dialog.vue";
import tableList from "../components/news/table.vue";
import Pagination from "../components/common/page2.vue";
import {
  NewsList,
  Addnews,
  Delnews,
  DelAllnews,
  getNews,
  newstype,
} from "../api/news";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    newsDialog,
    tableList,
    Pagination,
  },
  data() {
    //这里存放数据
    return {
      centerDialogVisible: false,
      isEdit: false,
      item: {},
      value: "",
      page: 1,
      pageSize: 5,
      key: "",
      type: null,
      total: 0,
      seleceList: [],
      id: [],
      list: [],
      tableList: [
        {
          label: "类型",
          prop: "typename",
        },
        {
          label: "标题",
          prop: "title",
        },
        {
          label: "图片",
          prop: "picture",
        },
        {
          label: "状态",
          prop: "state",
        },
        {
          label: "发布时间",
          prop: "addtime",
        },
        {
          label: "发布人",
          prop: "edituser",
        },
        {
          label: "操作",
          type: "button",
          button: {
            updata: true,
            delete: true,
          },
        },
      ],
    };
  },
  //方法集合
  methods: {
    addNews() {
      (this.isEdit = false), (this.centerDialogVisible = true);
    },
    //修改
    updata(val) {
      this.item = val;
      this.centerDialogVisible = true;
      this.isEdit = true;
    },
    //   分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getNews();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getNews();
    },
    // 复选框选中的id
    multipleSelection(val) {
      this.id = val;
    },
    //列表
    async getNews() {
      const { data } = await NewsList({
        page: this.page,
        psize: this.pageSize,
        key: this.key,
        type: this.type,
      });
      this.total = data.counts;
      this.list = data.list;
    },
    // 删除
    Newsdel(id) {
      console.log(id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(async () => {
          const { data } = await Delnews({ id: id });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getNews();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //批量删除
    NewsDelAll() {
      let ids = [];
      this.id.forEach((item) => {
        ids.push(item.id);
      });
      this.$confirm("是否删除选中的数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(async () => {
          const { data } = await DelAllnews({ ids: ids });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getNews();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    close(show, show2) {
      this[show] = false;
      this.isEdit = show2;
      this.getNews();
    },
    //搜索的下拉框
    async NewsType() {
      const { data } = await newstype();
      this.seleceList = data.list;
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getNews();
    this.NewsType();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
};
</script>
<style lang="less" scoped>
.residentBox {
  .box-card {
    padding-bottom: 20px;
    .item {
      float: left;
      :nth-child(2) {
        width: 180px;
        margin-left: 10px;
      }
      :nth-child(3) {
        margin-left: 30px;
        margin-right: 10px;
      }
    }
    .el-select {
      float: left;
    }
    .el-button {
      margin-left: 30px;
      width: 100px;
    }
  }
  .btn {
    margin-top: 14px;
    .el-button {
      width: 100px;
    }
  }
  .el-table {
    margin-top: 16px;
    .el-table-column {
      text-align: center;
    }
  }
  .el-pagination {
    margin-top: 8px;
    margin-left: 30%;
  }
}
</style>